package com.ff.ticket.controller;


import com.ff.ticket.domain.Actor;
import com.ff.ticket.domain.Show;
import com.ff.ticket.domain.User;
import com.ff.ticket.service.impl.ShowService;
import com.ff.ticket.service.impl.UserService;
import com.ff.ticket.util.Coder;
import com.ff.ticket.util.RandomValidateCodeUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/show")
public class ShowController {

    private final static Logger logger = LoggerFactory.getLogger(ShowController.class);

    @Resource
    private ShowService showService;


    @PostMapping("/create")
    @ApiOperation(value = "添加演出", notes = "添加演出")
    public Map<String, Object> createShow(@ApiParam(required = true, value = "演出名称") @RequestParam String showName,
                                          @ApiParam(required = false, value = "演出类型") @RequestParam(required = false) String showType,
                                          @ApiParam(required = false, value = "演出封面") @RequestParam(required = false) String showCover,
                                          @ApiParam(required = false, value = "演出价格区间") @RequestParam(required = false) String showPrice,
                                          @ApiParam(required = false, value = "销售状态") @RequestParam(required = false) String showStatus,
                                          @ApiParam(required = false, value = "演出时间") @RequestParam(required = false) String showTime,
                                          @ApiParam(required = false, value = "演出城市") @RequestParam(required = false) String showCity,
                                          @ApiParam(required = false, value = "演出场地") @RequestParam(required = false) String showField,
                                          @ApiParam(required = false, value = "详细地址") @RequestParam(required = false) String showAddress,
                                          @ApiParam(required = false, value = "参演艺人") @RequestParam(required = false) List<Actor> showActors,
                                          @ApiParam(required = false, value = "主办方") @RequestParam(required = false) String showOrganiser,
                                          @ApiParam(required = false, value = "艺人相册") @RequestParam(required = false) List<String> showPhotos,
                                          HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Show show = new Show();
        show.setShowName(showName);
        if (null != showType && showType.length() > 0) {
            show.setShowType(showType);
        }
        if (null != showCover && showCover.length() > 0) {
            show.setShowCover(showCover);
        }
        if (null != showPrice && showPrice.length() > 0) {
            show.setShowPrice(showPrice);
        }
        if (null != showStatus && showStatus.length() > 0) {
            show.setShowStatus(showStatus);
        }
        if (null != showTime && showTime.length() > 0) {
            show.setShowTime(showTime);
        }
        if (null != showCity && showCity.length() > 0) {
            show.setShowCity(showCity);
        }
        if (null != showField && showField.length() > 0) {
            show.setShowField(showField);
        }
        if (null != showAddress && showAddress.length() > 0) {
            show.setShowAddress(showAddress);
        }
        if (null != showActors && showActors.size() > 0) {
            show.setShowActors(showActors);
        }
        if (null != showOrganiser && showOrganiser.length() > 0) {
            show.setShowOrganiser(showOrganiser);
        }


        showService.updateShow(show);

        result.put("code", 10000);
        result.put("message", "演出添加成功");


        return result;

    }

    @PostMapping("/update")
    @ApiOperation(value = "更新演出", notes = "更新演出")
    public Map<String, Object> updateShow(@ApiParam(required = true, value = "演出id") @RequestParam String showId,
                                          @ApiParam(required = true, value = "演出名称") @RequestParam String showName,
                                          @ApiParam(required = false, value = "演出类型") @RequestParam(required = false) String showType,
                                          @ApiParam(required = false, value = "演出封面") @RequestParam(required = false) String showCover,
                                          @ApiParam(required = false, value = "演出价格区间") @RequestParam(required = false) String showPrice,
                                          @ApiParam(required = false, value = "销售状态") @RequestParam(required = false) String showStatus,
                                          @ApiParam(required = false, value = "演出时间") @RequestParam(required = false) String showTime,
                                          @ApiParam(required = false, value = "演出城市") @RequestParam(required = false) String showCity,
                                          @ApiParam(required = false, value = "演出场地") @RequestParam(required = false) String showField,
                                          @ApiParam(required = false, value = "详细地址") @RequestParam(required = false) String showAddress,
                                          @ApiParam(required = false, value = "参演艺人") @RequestParam(required = false) List<Actor> showActors,
                                          @ApiParam(required = false, value = "主办方") @RequestParam(required = false) String showOrganiser,
                                          @ApiParam(required = false, value = "艺人相册") @RequestParam(required = false) List<String> showPhotos,
                                          HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Show show = showService.findShowById(showId);
        if (null != show) {
            show.setShowName(showName);
            if (null != showType && showType.length() > 0) {
                show.setShowType(showType);
            } else {
                show.setShowType("");
            }
            if (null != showCover && showCover.length() > 0) {
                show.setShowCover(showCover);
            } else {
                show.setShowCover("");
            }
            if (null != showPrice && showPrice.length() > 0) {
                show.setShowPrice(showPrice);
            } else {
                show.setShowPrice("");
            }
            if (null != showStatus && showStatus.length() > 0) {
                show.setShowStatus(showStatus);
            } else {
                show.setShowStatus("");
            }
            if (null != showTime && showTime.length() > 0) {
                show.setShowTime(showTime);
            } else {
                show.setShowTime("");
            }
            if (null != showCity && showCity.length() > 0) {
                show.setShowCity(showCity);
            } else {
                show.setShowCity("");
            }
            if (null != showField && showField.length() > 0) {
                show.setShowField(showField);
            } else {
                show.setShowField("");
            }
            if (null != showAddress && showAddress.length() > 0) {
                show.setShowAddress(showAddress);
            } else {
                show.setShowAddress("");
            }
            if (null != showActors && showActors.size() > 0) {
                show.setShowActors(showActors);
            } else {
                show.setShowActors(null);
            }
            if (null != showOrganiser && showOrganiser.length() > 0) {
                show.setShowOrganiser(showOrganiser);
            } else {
                show.setShowOrganiser("");
            }
            showService.updateShow(show);

            result.put("code", 10000);
            result.put("message", "演出更新成功");

        } else {
            result.put("code", 10001);
            result.put("message", "未找到演出");
        }

        return result;

    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除演出", notes = "删除演出")
    public Map<String, Object> deleteShow(@ApiParam(required = true, value = "演出id") @RequestParam String showId) {

        Map<String, Object> result = new HashMap<>();
        Show show = showService.findShowById(showId);
        if (null != show) {
            showService.deleteShow(show);
            result.put("code", 10000);
            result.put("message", "演出删除成功");

        } else {
            result.put("code", 10001);
            result.put("message", "未找到演出");
        }

        return result;

    }

    @PostMapping("/search")
    @ApiOperation(value = "搜索演出", notes = "搜索演出")
    public Map<String, Object> searchShow(@ApiParam(required = false, value = "搜索条件") @RequestParam(required = false) String status,
                                          @ApiParam(required = false, value = "搜索条件") @RequestParam(required = false) String tag) {

        Map<String, Object> result = new HashMap<>();
        List<Show> list = showService.findShowsByStatusAndTag(status, tag);
        if (null != list && list.size()>0) {
            result.put("data",list);
            result.put("code", 10000);
            result.put("message", "获取演出列表成功");

        } else {
            result.put("code", 10001);
            result.put("message", "未找到演出");
        }

        return result;

    }
}
